// Written by HYD 2003/10/28

#define MAX_LENGTH	256
#define PORT		1453
#define H_SIZE		100 * 1024 * 1024
#define KEY		1101

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include "list.h"

void h_read( int sockfd );
void funcb( char *stockno );
void funcc(  );

int main(  )
{
  int shmid;
  char *p;

  if( ( shmid = shmget( KEY, H_SIZE, 0 ) ) < 0 &&
      ( shmid = shmget( KEY, H_SIZE, IPC_CREAT | 438 ) ) < 0 )
  {
    perror( "shm: " );
    return 0;
  }
  if( ( ltick = shmat( shmid, 0, 0 ) ) == ( void * ) -1 )
  {
    perror( "shmat: " );
    return 0;
  }
  printf
    ( "Content-type: text/html;charset=big5\n\n<html><head><meta http-equiv=Pragma content=no-cache><meta http-equiv=refresh content=30><script>function a(o){o.style.backgroundColor='#C8C8EC'}function b(o){o.style.backgroundColor='#ffffff'}function c(a){var b=open('stockview.cgi?stockno='+a,'stockview','toolbar=no,width=300,height=450,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no');b.focus();}</script></head><body><center><table border=1 cellspacing=0 cellpadding=0 style=font-size:8pt><caption><marquee>TSE SHM " );
  if( ( p = getenv( "QUERY_STRING" ) ) && *p && strlen( p ) > 11 )
  {
    printf( "%s stock trade list</marquee></caption>\n", p + 8 );
    funcb( p + 8 );
  }
  else
  {
    printf( "VIEW</marquee></caption>\n" );
    funcc(  );
  }
  printf( "</table></center></body></html>\n" );
  shmdt( ( void * ) ltick );
  return 0;
}

void funcb( char *stockno )
{
  int y, z;

  for( z = 0; z < NUM_EMPL; z++ )
  {
    if( !strncmp( ltick->DIFF_num[z].stockno, stockno, strlen( stockno ) ) &&
        ( ltick->DIFF_num[z].stockno[strlen( stockno )] == 32
          || !ltick->DIFF_num[z].stockno[strlen( stockno )] ) )
    {
      printf
        ( "<tr bgcolor=#C0C0FF><th>TIME<th>SN<th>Change<th>Buy_quantity<th>Sell_quantity<th>Bid<th>Ask</tr>\n" );
      for( y = 0; y < MAX_NUM; y++ )
      {
        if( !*ltick->DIFF_num[z].LDIFF_num[y].ticktime )
        {
          break;
        }
        printf
          ( "<tr bgcolor='#ffffff' onmouseover=a(this) onmouseout=b(this)><td>%s<td>%s<td>%s<td>%s<td>%s<td>%s<td>%s</tr>",
            ltick->DIFF_num[z].LDIFF_num[y].ticktime,
            ltick->DIFF_num[z].LDIFF_num[y].serial,
            ltick->DIFF_num[z].LDIFF_num[y].pricediff,
            ltick->DIFF_num[z].LDIFF_num[y].buyvolume,
            ltick->DIFF_num[z].LDIFF_num[y].sellvolume,
            ltick->DIFF_num[z].LDIFF_num[y].buyprice,
            ltick->DIFF_num[z].LDIFF_num[y].sellprice );
      }
      break;
    }
  }
}

char *h_convert( char *stockno, char *temp )
{
  int z;

  if( !stockno || !*stockno )
  {
    return "";
  }
  strncpy( temp, stockno, 7 );
  for( z = 0; z < strlen( stockno ); z++ )
  {
    if( stockno[z] == 32 )
    {
      temp[z] = 0;
      break;
    }
  }
  return temp;
}

void funcc(  )
{
  char temp[8], *head =
    "<tr bgcolor=#C0C0FF><th nowrap>no<th nowrap>stockno<th nowrap>stock_name<th nowrap>refprice<th nowrap>dealprice<th nowrap>pricediff<th nowrap>dealvolume<th nowrap>openprice<th nowrap>maxprice<th nowrap>minprice<th nowrap>maximum<th nowrap>minimum<th nowrap>ticktime<th nowrap>serial<th nowrap>diff<th nowrap>deal<th nowrap>amp<th nowrap>buyprice0<th nowrap>buyprice1<th nowrap>buyprice2<th nowrap>buyprice3<th nowrap>buyprice4<th nowrap>buyvolume0<th nowrap>buyvolume1<th nowrap>buyvolume2<th nowrap>buyvolume3<th nowrap>buyvolume4<th nowrap>sellvolume0<th nowrap>sellvolume1<th nowrap>sellvolume2<th nowrap>sellvolume3<th nowrap>sellvolume4<th nowrap>sellprice0<th nowrap>sellprice1<th nowrap>sellprice2<th nowrap>sellprice3<th nowrap>sellprice4</tr>";
  int z, counter = 0;

  for( z = 0; z < NUM_EMPL; z++ )
  {
    if( !*ltick->DIFF_num[z].stockno )
    {
      continue;
    }
    if( !( counter++ % 20 ) )
    {
      printf( head );
    }
    printf
      ( "<a href=# onclick=c('%s')><tr bgcolor='#ffffff' onmouseover=a(this) onmouseout=b(this)><th bgcolor=#C0C0FF align=right>%d<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%f<td nowrap>%ld<td nowrap>%f<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s<td nowrap>%s</tr></a>",
        h_convert( ltick->DIFF_num[z].stockno, temp ), counter,
        ltick->DIFF_num[z].stockno, ltick->DIFF_num[z].stock_name,
        ltick->DIFF_num[z].refprice, ltick->DIFF_num[z].dealprice,
        ltick->DIFF_num[z].pricediff, ltick->DIFF_num[z].dealvolume,
        ltick->DIFF_num[z].openprice, ltick->DIFF_num[z].maxprice,
        ltick->DIFF_num[z].minprice, ltick->DIFF_num[z].maximum,
        ltick->DIFF_num[z].minimum, ltick->DIFF_num[z].ticktime,
        ltick->DIFF_num[z].serial, ltick->DIFF_num[z].diff,
        ( long ) ltick->DIFF_num[z].deal, ltick->DIFF_num[z].amp,
        ltick->DIFF_num[z].buyprice[0], ltick->DIFF_num[z].buyprice[1],
        ltick->DIFF_num[z].buyprice[2], ltick->DIFF_num[z].buyprice[3],
        ltick->DIFF_num[z].buyprice[4], ltick->DIFF_num[z].buyvolume[0],
        ltick->DIFF_num[z].buyvolume[1], ltick->DIFF_num[z].buyvolume[2],
        ltick->DIFF_num[z].buyvolume[3], ltick->DIFF_num[z].buyvolume[4],
        ltick->DIFF_num[z].sellvolume[0], ltick->DIFF_num[z].sellvolume[1],
        ltick->DIFF_num[z].sellvolume[2], ltick->DIFF_num[z].sellvolume[3],
        ltick->DIFF_num[z].sellvolume[4], ltick->DIFF_num[z].sellprice[0],
        ltick->DIFF_num[z].sellprice[1], ltick->DIFF_num[z].sellprice[2],
        ltick->DIFF_num[z].sellprice[3], ltick->DIFF_num[z].sellprice[4] );
  }
}
